package com.koron.css2.newProjectCenter.callback;

import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.service.PjInfoService;
import com.koron.css2.newProjectCenter.vo.gcjs.GJResponseData;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.HttpUtil;
import com.koron.util.JsonUtils;
import com.koron.util.secret.StaticInfo;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import static com.koron.css2.newProjectCenter.service.gcjsData.gcjsDataModel;

/**
 * 资料审核节点回调
 */
@Service(PjInfoService.INFO_APPROVAL)
public class InfoApprovalCallback implements IPjInfoNodeCallback{

    private static Logger logger = LoggerFactory.getLogger(InfoApprovalCallback.class);

    /**
     * 是否上传工建系统-是
     */
    public final static String IS_UPLOAD_WORK_TRUE = "1";

    @Override
    public MessageBean callback(SessionFactory factory, PjInfoBean pjInfoBean, UserInfoBean userInfo) {
        PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
        CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
        AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
        /**
         * 如果是资料审核节点，则将数据封装后传给工建系统
         * 暂时写死只同步信宜水司，后续其他水司有此需求再创建参数或者字典控制
         * 20230805  新需求  只同步企业报装(删除此逻辑)
         * 20230814 新需求  根据页面是否上传工建字段确定 是否同步数据到工建系统
         */
        if (GCJS_COMPANY.equals(userInfo.getCurWaterCode()) && pjInfoBean.getGcjsStatic() == 0 && IS_UPLOAD_WORK_TRUE.equals(pjInfoBean.getIsUploadWorkBuild())){
            try {
                CtmInfoBean ctmInfo = ctmInfoMapper.getCtmInfoById(pjInfoBean.getUserCtmTempId(), 2);
                AccountInfoBean accountInfo = accountInfoMapper.getAccountInfoById(pjInfoBean.getUserAccountTempId(), 2);
                String s1 = gcjsDataModel(userInfo,pjInfoBean, ctmInfo, accountInfo);
                logger.info("报装数据推送至工建参数："+s1);
                String s = HttpUtil.sendPost(StaticInfo.interBusiApi+"/gcjs/dataReception",s1);
                GJResponseData responseData = JsonUtils.jsonToPojo(s,GJResponseData.class);
                if (responseData.getErrcode()!=GJResponseData.SUCCESS_CODE){
                    logger.error("数据推送至工建失败,"+responseData.getMessage());
                }else {
                    //受理编号
                    String slbh = String.valueOf(responseData.getData());
                    if (StringUtils.isNotBlank(slbh)) {
                        logger.info("信宜工建返回受理编号：" + slbh);
                        //将受理编号插入数据库保存
                        pjInfoMapper.updateGcjsSlbhById(pjInfoBean.getId(), slbh);
                    }else {
                        logger.error("工建系统返回受理编号为空,"+responseData.getMessage());
                    }
                }
            }catch (Exception e){
                logger.error("信宜报装数据推送到工建系统失败",e);
            }
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
    }
}
